package java44th.group.controller.wx.coupon;

import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.admin.ListVo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketCoupon;
import java44th.group.bean.po.MarketUser;
import java44th.group.bean.wx.bo.coupon.CodeBo;
import java44th.group.bean.wx.bo.coupon.CouponId;
import java44th.group.service.wx.coupon.MarketCouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.LinkedList;

/**
 * @Aurhor: 王可龙
 * @DATE: 2022/10/15 14:05
 * @Package:java44th.group.controller.wx.coupon
 * @Description:
 * @Version 1.0
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {


    @Autowired
    MarketCouponService marketCouponService;


    @RequestMapping("list")
    public BaseRespVo list(Integer page,Integer limit){
        ListVo list=marketCouponService.list(page,limit);
        return BaseRespVo.ok(list);
    }

    @RequestMapping("mylist")
    public BaseRespVo myList(Integer status,Integer page,Integer limit){
        //获取当前用户的id
        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();
        //更新当前用户id的优惠卷的状态
        marketCouponService.updateStatusByUserId(userId);
        //得到当前用户的id下所有状态为（status）0 优惠卷
        ListVo list=marketCouponService.myList(userId,status,page,limit);
        return BaseRespVo.ok(list);
    }

    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody CouponId couponId) throws ParseException {
        //获取当前用户的id
        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer id = marketUser.getId();
        Integer receive = marketCouponService.receive(id, couponId);
        if (receive==1){
            return BaseRespVo.error(740,"优惠券已经领取过");
        }
        return BaseRespVo.ok();
    }


    @RequestMapping("selectlist")
    public BaseRespVo selectlist(){
        //获取当前用户的id
        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer id = marketUser.getId();
        System.out.println(id);

        ListVo list=marketCouponService.selectlist(id);
        return BaseRespVo.ok(list);
    }

    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody CodeBo codeBo) throws ParseException {
        //获取当前用户的id
        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer id = marketUser.getId();
        System.out.println(id);
        Integer i=marketCouponService.exchange(id,codeBo);
        if (i==1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.error(742,"优惠券不正确");
    }


}
